function fetchWithAbortTimeout(url, options, timeout = 5000) {
    const controller = new AbortController();
    const { signal } = controller;

    const fetchPromise = fetch(url, { ...options, signal });
    const timeoutPromise = new Promise((_, reject) => {
        setTimeout(() => {
            controller.abort(); // 取消请求
            reject(new Error("Request timeout"));
        }, timeout);
    });

    return Promise.race([fetchPromise, timeoutPromise]);
}

// 使用示例
fetchWithAbortTimeout("https://api.example.com/data", {}, 3000)
    .then(response => response.json())
    .then(data => console.log(data))
    .catch(error => console.error(error.message)); // 超时或取消会进入这里